<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/1/11
 * Time: 9:22
 */
namespace App\Http\Controllers\Api\V3;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Modules\EntMemberInfo;
use App\Modules\EntAwardPlan;
use App\Modules\EntReadingParty;
use App\Modules\EntMemberMoney;
use App\Modules\EntMoneyRewardLog;
use App\Modules\EntAwardMoneyLog;
use Validator, DB;

use App\Models\V1\MessageModel;
use App\Common\Jpush;
use App\Models\V1\BookModel;
use App\Models\V1\MemberModel;

use App\Models\V1\ReadPartyModel;
use App\Common\ApiData;



class AwardPlanController extends BaseController
{

    /**
     * 查看学习币详细
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getAwardPlanDetail(Request $request)
    {
        $result = 'no';
        $rules = [
            'id' => 'required',
            'mobile' => 'required',
            'key' => 'required'
        ];
        $messages = [
            'id.required' => '2039',
            'mobile.required' => '2037',
            'key.required' => '2038'
        ];
        $id = $request->input('id');
        $mobile = $request->input('mobile');
        $key = $request->input('key');
        $params = ['mobile' => $mobile, 'key' => $key, 'id' => $id];
        $v = Validator::make($params, $rules, $messages);
        if ($v->fails()) {
            return response()->json(['message' => $result, 'code' => $v->messages()->first()]);
        }
        $memberModel    = new MemberModel();
        $member         = $memberModel->get_member_info_by_mobile_key($mobile,$key);

        if ($member != false) {
            $entReadingParty = EntReadingParty::find($id);
            if (count($entReadingParty) > 0) {
                if ($entReadingParty->MemNumber == $member->MemNumber) {
                    $arrryMoney = array();
                    $moneyOne = 0;
                    $moneyTwo = 0;
                    $awardPlanDetail = EntAwardPlan::select('AwaPlanID', 'AwaPlanType', 'ReaParID', 'AwaPlanMoney')->where('ReaParID', $id)->where('MemNumber', $member->MemNumber)->get();
                    if (count($awardPlanDetail)) {
                        foreach ($awardPlanDetail as $k) {
                            switch ($k->AwaPlanType) {
                                case 1:
                                    $moneyOne = $k->AwaPlanMoney;
                                    break;
                                case 2:
                                    $moneyTwo = $k->AwaPlanMoney;
                                    break;
                            }
                        }
                        $arrryMoney = ['moneyone' => $moneyOne, 'moneytwo' => $moneyTwo];
                        return response()->json(['message' => 'ok', 'code' => '200', 'moneydetail' => $arrryMoney]);
                    } else {
                        $arrryMoney = ['moneyone' => 0, 'moneytwo' => 0];
                        return response()->json(['message' => 'ok', 'code' => '200', 'moneydetail' => $arrryMoney]);
                    }
                } else {
                    //会员权限不足
                    return response()->json(['message' => $result, 'code' => '3087']);
                }
            } else {
                //读书会信息不存在
                return response()->json(['message' => $result, 'code' => '3086']);
            }
        } else {
            return response()->json(['message' => $result, 'code' => '3027']);
        }
    }

    /**
     * 添加读书奖励计划
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function awardPlanStore(Request $request)
    {
        $result = 'no';
        $rules = [
            'id' => 'required',
            'planstr' => 'required',
            'mobile' => 'required',
            'key' => 'required',
            'timestamp' => 'required',
            'sign' => 'required'
        ];
        $messages = [
            'id.required' => '6005',
            'planstr.required' => '6006',
            'mobile.required' => '2037',
            'key.required' => '2038',
            'timestamp.required' => '2040',
            'sign.required' => '2041'
        ];
        $id = $request->input('id');
        $planstr = $request->input('planstr');
        $mobile = $request->input('mobile');
        $key = $request->input('key');
        $sign = $request->input('sign');
        $timestamp = $request->input('timestamp');
        $params = ['mobile' => $mobile, 'key' => $key, 'id' => $id, 'planstr' => $planstr, 'timestamp' => $timestamp, 'sign' => $sign];
        $v = Validator::make($params, $rules, $messages);
        if ($v->fails()) {
            return response()->json(['message' => $result, 'code' => $v->messages()->first()]);
        }
        if ($this->checkSign($params) == true) {

            $member = $this->memNumber($mobile, $key);
            if (count($member) > 0) {
                $entReadingParty = EntReadingParty::find($id);
                if ($entReadingParty->MemNumber == $member->MemNumber) {
                    $typeOne = '';
                    $moneyOne = '';
                    $typeTwo = '';
                    $moneyTwo = '';
                    if (strstr($planstr, '|') == true) {
                        $arrayplanstr = explode('|', $planstr);
                        foreach ($arrayplanstr as $k => $v) {
                            $arrayplanstrTwo = explode('_', $v);
                            switch ($k) {
                                case 0:
                                    $typeOne = $arrayplanstrTwo[0];
                                    $moneyOne = $arrayplanstrTwo[1];
                                    break;
                                case 1:
                                    $typeTwo = $arrayplanstrTwo[0];
                                    $moneyTwo = $arrayplanstrTwo[1];
                                    break;
                            }
                        }
                    }
                    if ($typeOne != '' && $moneyOne != '' && $typeTwo != '' && $moneyTwo != '') {
                        DB::beginTransaction();
                        try {
                            $checkAwardPlan = EntAwardPlan::select('AwaPlanID')->where('MemNumber', $member->MemNumber)->where('AwaPlanType', $typeOne)->where('ReaParID', $id)->first();
                            //阅读星期三书籍
                            if (count($checkAwardPlan) == 0) {
                                $entAwardPlan = new EntAwardPlan();
                                $entAwardPlan->AwaPlanType = $typeOne;
                                $entAwardPlan->ReaParID = $id;
                                $entAwardPlan->AwaPlanMoney = $moneyOne;
                                $entAwardPlan->MemNumber = $member->MemNumber;
                                $entAwardPlan->ReleaseDate = date('Y-m-d H:i:s', time());
                                $entAwardPlan->AddDate = date('Y-m-d H:i:s', time());
                                $entAwardPlan->save();
                                if ($entAwardPlan) {
                                    //添加成功
                                    //return response()->json(['message' => 'ok', 'code' => '200']);
                                    $i = 1;
                                } else {
                                    //添加失败
                                    //return response()->json(['message' => $result, 'code' => '3084']);
                                    $i = 2;
                                }
                            } else {
                                $entAwardPlan = EntAwardPlan::find($checkAwardPlan->AwaPlanID);
                                $entAwardPlan->AwaPlanMoney = $moneyOne;
                                $entAwardPlan->ReleaseDate = date('Y-m-d H:i:s', time());
                                $entAwardPlan->save();
                                if ($entAwardPlan) {
                                    //修改成功
                                    //return response()->json(['message' => 'ok', 'code' => '200']);
                                    $i = 3;
                                } else {
                                    //修改失败
                                    //return response()->json(['message' => $result, 'code' => '3083']);
                                    $i = 4;
                                }
                            }
                            $checkAwardPlanTwo = EntAwardPlan::select('AwaPlanID')->where('MemNumber', $member->MemNumber)->where('AwaPlanType', $typeTwo)->where('ReaParID', $id)->first();
                            //添加改进计划
                            if (count($checkAwardPlanTwo) == 0) {
                                $entAwardPlanTwo = new EntAwardPlan();
                                $entAwardPlanTwo->AwaPlanType = $typeTwo;
                                $entAwardPlanTwo->ReaParID = $id;
                                $entAwardPlanTwo->AwaPlanMoney = $moneyTwo;
                                $entAwardPlanTwo->MemNumber = $member->MemNumber;
                                $entAwardPlanTwo->ReleaseDate = date('Y-m-d H:i:s', time());
                                $entAwardPlanTwo->AddDate = date('Y-m-d H:i:s', time());
                                $entAwardPlanTwo->save();
                                if ($entAwardPlanTwo) {
                                    //添加成功
                                    //return response()->json(['message' => 'ok', 'code' => '200']);
                                    $i = 5;
                                } else {
                                    //添加失败
                                    //return response()->json(['message' => $result, 'code' => '3082']);
                                    $i = 6;
                                }
                            } else {
                                $entAwardPlanTwo = EntAwardPlan::find($checkAwardPlanTwo->AwaPlanID);
                                $entAwardPlanTwo->AwaPlanMoney = $moneyTwo;
                                $entAwardPlanTwo->ReleaseDate = date('Y-m-d H:i:s', time());
                                $entAwardPlanTwo->save();
                                if ($entAwardPlanTwo) {
                                    //修改成功
                                    //return response()->json(['message' => 'ok', 'code' => '200']);
                                    $i = 7;
                                } else {
                                    //修改失败
                                    //return response()->json(['message' => $result, 'code' => '3081']);
                                    $i = 8;
                                }
                            }
                            DB::commit();
                        } catch (Exception $e) {
                            DB::rollBack();
                            $i = 0;
                        }
                        switch ($i) {
                            case 0:
                                return response()->json(['message' => $result, 'code' => '3085']);
                                break;
                            case 1:
                                return response()->json(['message' => 'ok', 'code' => '200']);
                                break;
                            case 2:
                                return response()->json(['message' => $result, 'code' => '3084']);
                                break;
                            case 3:
                                return response()->json(['message' => 'ok', 'code' => '200']);
                                break;
                            case 4:
                                return response()->json(['message' => $result, 'code' => '3083']);
                                break;
                            case 5:
                                return response()->json(['message' => 'ok', 'code' => '200']);
                                break;
                            case 6:
                                return response()->json(['message' => $result, 'code' => '3082']);
                                break;
                            case 7:
                                return response()->json(['message' => 'ok', 'code' => '200']);
                                break;
                            case 8:
                                return response()->json(['message' => $result, 'code' => '3081']);
                                break;
                        }
                    } else {
                        //字符串拼接错误
                        return response()->json(['message' => $result, 'code' => '3080']);
                    }
                } else {
                    //权限不足
                    return response()->json(['message' => $result, 'code' => '3079']);
                }
            } else {
                return response()->json(['message' => $result, 'code' => '3027']);
            }
        } else {
            return response()->json(['message' => $result, 'code' => '3028']);
        }
    }



    /**
     * 单个打赏
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function singleSanStore(Request $request)
    {
        $result     = 'no';
        $rules      = [
            'id'        => 'required',
            'type'      => 'required',
            'memid'     => 'required',
            'amount'    => 'required',
            'mobile'    => 'required',
            'key'       => 'required',
            'timestamp' => 'required',
            'sign'      => 'required'
        ];
        $messages   = [
            'id.required'       => '2103',
            'type.required'     => '2104',
            'memid.required'    => '2105',
            'amount.required'   => '2106',
            'mobile.required'   => '2037',
            'key.required'      => '2038',
            'timestamp.required'=> '2040',
            'sign.required'     => '2041'
        ];
        $id         = $request->input('id');
        $type       = $request->input('type');
        $memid      = $request->input('memid');
        $amount     = $request->input('amount');
        $mobile     = $request->input('mobile');
        $key        = $request->input('key');
        $sign       = $request->input('sign');
        $timestamp  = $request->input('timestamp');

        $params     = [
            'mobile'    => $mobile,
            'key'       => $key,
            'id'        => $id,
            'type'      => $type,
            'memid'     => $memid,
            'amount'    => $amount,
            'timestamp' => $timestamp,
            'sign'      => $sign
        ];

        $v = Validator::make($params, $rules, $messages);
        if ($v->fails()) {
            return response()->json(['message' => $result, 'code' => $v->messages()->first()]);
        }
        if ($this->checkSign($params) != true) {
            return response()->json(['message' => $result, 'code' => '3028']);
        }

        $memberModel    = new MemberModel();
        $member         = $memberModel->get_member_info_by_mobile_key($mobile,$key);

        if ( $member == false) {
            return response()->json(['message' => $result, 'code' => '3027']);
        }

        $u = 0;
        $commentId = $request->input('commentId');
        $giftData  = [
            'comment_id'=>$commentId,
            'user_id'   =>$member->UserID,
            'money'     =>$amount,
            'read_party_id'=>$id
        ];



        if($type == 3){
            if ($member->MemNumber == $memid) { //权限不足被 不能自己给自己打赏
                $u = 14;
                goto end;
            }

            $entMemberMoney = EntMemberMoney::select('StuMonID', 'StudyMoney')
                ->where('ReaParID', $id)
                ->where('MemNumber', $member->MemNumber)
                ->first();

            if (count($entMemberMoney) == 0) { //没有学习币
                $u = 5;
                goto end;
            }


            if ($entMemberMoney->StudyMoney < $amount) {//学习币不足
                $u = 4;
                goto end;
            }

            DB::beginTransaction();
            try {
                /*给被打赏的人添加学习币*/
                $beiMemberMoney = EntMemberMoney::select('StuMonID', 'StudyMoney')
                    ->where('ReaParID', $id)
                    ->where('MemNumber', $memid)
                    ->first();
                if (count($beiMemberMoney) > 0) { //更新被打赏人学习币
                    $increaseOutStudyMoney = $beiMemberMoney->StudyMoney + $amount;
                    DB::table('MemberMoney')
                        ->where('StuMonID', $beiMemberMoney->StuMonID)
                        ->update(['StudyMoney' => $increaseOutStudyMoney]);
                } else {
                    /*添加读书会学习币*/
                    $entAddMemberMoney = new EntMemberMoney();
                    $entAddMemberMoney->ReaParID    = $id;
                    $entAddMemberMoney->StudyMoney  = $amount;
                    $entAddMemberMoney->MemNumber   = $memid;
                    $entAddMemberMoney->ReleaseDate = date('Y-m-d H:i:s', time());
                    $entAddMemberMoney->AddDate     = date('Y-m-d H:i:s', time());
                    $entAddMemberMoney->save();
                }

                /*扣除打赏人学习币*/
                $takeOutStudyMoney = $entMemberMoney->StudyMoney - $amount;
                DB::table('MemberMoney')
                    ->where('StuMonID', $entMemberMoney->StuMonID)
                    ->update(['StudyMoney' => $takeOutStudyMoney]);


                /*添加学习币变动日志*/
                $entMoneyRewardLog = new EntMoneyRewardLog();
                $entMoneyRewardLog->MoReLoType      = 3;
                $entMoneyRewardLog->read_party_id   = $id;
                $entMoneyRewardLog->MoReLoDesc      = '打赏会员扣除 ' . $amount . ' 个学习币。';
                $entMoneyRewardLog->MoReLoBeiDesc   = '被会员打赏获得 ' . $amount . ' 个学习币。';
                $entMoneyRewardLog->MoReLoMoney     = $amount;
                $entMoneyRewardLog->BeiMemNumber    = $memid;
                $entMoneyRewardLog->MemNumber       = $member->MemNumber;
                $entMoneyRewardLog->AddDate         = date('Y-m-d H:i:s', time());
                $entMoneyRewardLog->save();
                $u = 1;
                $giftData['money_log_id'] = $entMoneyRewardLog->MoReLoID;
                DB::commit();
            } catch (Exception $e) {
                DB::rollBack();
                $u = 3;     //打赏失败
            }

        }elseif($type == 4){    /*特指管理员打赏 管理员打赏不用考量剩余的学习币是否足够*/

            $entReadingParty = EntReadingParty::find($id);
            if ($entReadingParty->MemNumber != $member->MemNumber) { /*打赏权限不足*/
                $u = 13;
                goto end;
            }

            DB::beginTransaction();
            try {
                $checkMemberMoney = EntMemberMoney::select('StuMonID', 'StudyMoney')
                    ->where('ReaParID', $id)
                    ->where('MemNumber', $memid)
                    ->first();

                if (count($checkMemberMoney) > 0) { //检查会员是否已经有了记录
                    $studyMoney = $checkMemberMoney->StudyMoney + $amount;
                    DB::table('MemberMoney')
                        ->where('StuMonID', $checkMemberMoney->StuMonID)
                        ->update(['StudyMoney' => $studyMoney]);
                } else {
                    //添加读书会学习币
                    $entMemberMoney = new EntMemberMoney();
                    $entMemberMoney->ReaParID = $id;
                    $entMemberMoney->StudyMoney = $amount;
                    $entMemberMoney->MemNumber = $memid;
                    $entMemberMoney->ReleaseDate = date('Y-m-d H:i:s', time());
                    $entMemberMoney->AddDate = date('Y-m-d H:i:s', time());
                    $entMemberMoney->save();
                }

                //添加会长打赏记录
                $entMoneyRewardLog = new EntMoneyRewardLog();
                $entMoneyRewardLog->MoReLoType      = 4;
                $entMoneyRewardLog->read_party_id   = $id;
                $entMoneyRewardLog->MoReLoDesc      = '打赏会员 ' . $amount . ' 个学习币。';
                $entMoneyRewardLog->MoReLoBeiDesc   = '被会长打赏获得 ' . $amount . ' 个学习币。';
                $entMoneyRewardLog->MoReLoMoney     = $amount;
                $entMoneyRewardLog->BeiMemNumber    = $memid;
                $entMoneyRewardLog->MemNumber       = $member->MemNumber;
                $entMoneyRewardLog->AddDate         = date('Y-m-d H:i:s', time());
                $entMoneyRewardLog->save();
                $u = 1;
                $giftData['money_log_id'] = $entMoneyRewardLog->MoReLoID;
                DB::commit();
            } catch (Exception $e) {
                DB::rollBack();
                $u = 12;
            }
        }

        /*==============*/
        end:
        if($u == 1){
            $memberInfo     = $memberModel->get_member_info_by_member_number($memid);
            $bookModel  = new BookModel();
            $commentInfo= $bookModel->get_comment_info($commentId);
            $readModel = new EntReadingParty();
            $readModel->add_read_plan_money_gift($giftData);
            $title          = "打赏学习币";
            $msg            = $member->YourName."给你的《".$commentInfo->BookTitle."》读书改进计划打赏了".$amount."个学习币!";
            $messageModel   = new MessageModel();
            $msgId          = $messageModel->add_note_message($title,$msg);
            $messageModel->add_user_message($msgId,$memberInfo->UserID);

            $jpush          = new Jpush();
            $jpush->alias   = $memberInfo->UserName;
            $jpush->set_message($msg);
            $jpush->send();
        }

        switch ($u) {
            case 1:
                return response()->json(['message' => 'ok', 'code' => '200']);
                break;
            case 2:
                return response()->json(['message' => $result, 'code' => '3088']);
                break;
            case 3:
                return response()->json(['message' => $result, 'code' => '3089']);
                break;
            case 4:
                return response()->json(['message' => $result, 'code' => '3090']);
                break;
            case 5:
                return response()->json(['message' => $result, 'code' => '3091']);
                break;
            case 7:
                return response()->json(['message' => $result, 'code' => '3088']);
                break;
            case 9:
                return response()->json(['message' => $result, 'code' => '3092']);
                break;
            case 11:
                return response()->json(['message' => $result, 'code' => '3093']);
                break;
            case 12:
                return response()->json(['message' => $result, 'code' => '3094']);
                break;
            case 13:
                return response()->json(['message' => $result, 'code' => '3095']);
                break;
            case 14:
                return response()->json(['message' => $result, 'code' => '3100']);
                break;
        }
    }


    /**
     * 给学习计划打赏
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * */
    public function award_read_plan(Request $request){

        define('_qiye_',1);
        define('_geren_',2);



        $apiData    = new ApiData();
        $readPartyId= $request->input('id');
        $type       = $request->input('type');
        $memid      = $request->input('memid');
        $amount     = $request->input('amount');
        $mobile     = $request->input('mobile');
        $key        = $request->input('key');
        $commentId  = $request->input('commentId');
        $sign       = $request->input('sign');
        $timestamp  = $request->input('timestamp');

        $params     = [
            'mobile'    => [$mobile,"缺少手机号","2037"],
            'key'       => [$key,"缺少设备ID","2038"],
            'id'        => [$readPartyId,"缺少读书会ID","2103"],
            'type'      => [$type,"缺少打赏类型","2104"],
            'memid'     => [$memid,"缺少会员编号","2105"],
            'amount'    => [$amount,"缺少打赏数量","2106"],
            'commentId' => [$commentId,"缺少读书计划ID","2109"]
        ];

        $checkResult    = $this->check_params($params);
        if($checkResult->status == false){
            $apiData->message   = $checkResult->message;
            $apiData->code      = $checkResult->code;
            return $apiData->send();
        }

        $memberModel    = new MemberModel();
        $memberInfo     = $memberModel->get_member_info_by_mobile_key($mobile,$key);
        if ( $memberInfo == false) {
            $apiData->message   = "会员不存在";
            $apiData->code      = "3027";
            return $apiData->send();
        }


        //1.不能自己给自己打赏
        if($memberInfo->MemNumber == $memid){
            $apiData->message   = "不能给自己打赏";
            $apiData->code      = "3100";
            return $apiData->send();
        }

        //2.检查读书会是否存在
        $readPartyModel     = new ReadPartyModel();
        $readPartyInfo      = $readPartyModel->get_read_party_info($readPartyId);
        if($readPartyInfo == false){
            $apiData->message   = "读书会不存在";
            $apiData->code      = "3095";
            return $apiData->send();
        }

        $readPartyType  = $readPartyInfo->ReaParType;

        $adminAward     = false;         //属于管理员打赏 打赏者和读书会管理员是同一个人
        if($readPartyType == _geren_){
            if($readPartyInfo->MemNumber = $memberInfo->MemNumber){
                $adminAward = true;
            }
        }elseif($readPartyType == _qiye_){
            $adminAward = $readPartyModel->check_have_award_right($readPartyId,$memberInfo->UserID);
        }


        if($adminAward){ /*特指管理员打赏 管理员打赏不用考量剩余的学习币是否足够*/
            //添加会长打赏记录 日志打赏记录信息
            $logMsg     = '打赏会员 ' . $amount . ' 个学习币。';
            $beiLogMsg  = '被会长打赏获得 ' . $amount . ' 个学习币。';
        }else{ //会员之间互相打赏
            //该会员在本读书会的学习币
            $readPartyMoney = $readPartyModel->get_member_money($memberInfo->MemNumber,$readPartyId);
            if($readPartyMoney < $amount){
                $apiData->message   = "您的学习币不足";
                $apiData->code      = "3089";
                return $apiData->send();
            }
            //添加打赏记录 日志打赏记录信息
            $logMsg     = '打赏会员扣除 ' . $amount . ' 个学习币。';
            $beiLogMsg  = '被会员打赏获得 ' . $amount . ' 个学习币。';
        }

        //会员之间互相打赏
        if($type == 3){

        }elseif($type == 4){    /*特指管理员打赏 管理员打赏不用考量剩余的学习币是否足够*/

        }


        //学习币获得者的信息
        $memberInfoGeter    = $memberModel->get_member_info_by_member_number($memid); //获得学习币的人的信息


        //5.1添加会员的总的学习币
        $result = $readPartyModel->add_read_plan_money($readPartyId,$memberInfoGeter->MemNumber,$amount);

        //5.2不是会长，扣除打赏者的学习币
        if($adminAward == false){
            $readPartyModel->add_read_plan_money($readPartyId,$memberInfo->MemNumber,-$amount);
        }


        //5.3添加学习币改变日志
        $arawLogData    = [
            'MoReLoType'    => $type,
            'read_party_id' => $readPartyId,
            'MemNumber'     => $memberInfo->MemNumber,
            'BeiMemNumber'  => $memberInfoGeter->MemNumber,
            'MoReLoMoney'   => $amount,
            'MoReLoDesc'    => $logMsg,
            'MoReLoBeiDesc' => $beiLogMsg,
        ];
        $logId = $readPartyModel->add_money_change_log($arawLogData);

        //5.4添加评论的打赏记录
        $giftData  = [
            'comment_id'    => $commentId,
            'user_id'       => $memberInfo->UserID,
            'money'         => $amount,
            'read_party_id' => $readPartyId,
            'money_log_id'  => $logId
        ];
        $readPartyModel->add_read_plan_money_gift($giftData);

        //5.5推送消息 与 添加消息
        $bookModel      = new BookModel();
        $commentInfo    = $bookModel->get_comment_info($commentId);
        $title          = "打赏学习币";
        $msg            = $memberInfo->YourName."给你的《".$commentInfo->BookTitle."》读书改进计划打赏了".$amount."个学习币!";
        $messageModel   = new MessageModel();
        $msgId          = $messageModel->add_note_message($title,$msg);
        $messageModel->add_user_message($msgId,$memberInfoGeter->UserID);

        $jpush          = new Jpush();
        $jpush->alias   = $memberInfoGeter->UserName;
        $jpush->set_message($msg);
        $jpush->send();

        $apiData->message   = "打赏成功！";
        $apiData->code      = "200";
        return $apiData->send();
    }



    /**
     * 批量更新学习币
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function blukSingleSanStore(Request $request)
    {
        //读书会类型
        define('_qiye_',1);
        define('_geren_',2);

        $apiData= new ApiData();
        $id     = $request->input('id');        //读书会ID
        $idlist = $request->input('idlist');    //会员IDS
        $amount = $request->input('amount');    //学习币
        $mobile = $request->input('mobile');
        $key    = $request->input('key');
        $sign   = $request->input('sign');
        $timestamp = $request->input('timestamp');

        $params = [
            'mobile'    => [$mobile,"缺少手机号","2037"],
            'key'       => [$key,"缺少设备ID","2038"],
            'id'        => [$id,"缺少读书会ID","2039"],
            'idlist'    => [$idlist,"会员列表","2101"],
            'amount'    => [$amount,"缺少学习币","2102"]
        ];

        $checkResult    = $this->check_params($params);
        if($checkResult->status == false){
            $apiData->message   = $checkResult->message;
            $apiData->code      = $checkResult->code;
            return $apiData->send();
        }


        $memberModel        = new MemberModel();
        $readPartyManager   = $memberModel->get_member_info_by_mobile_key($mobile,$key);
        if ($readPartyManager == false) {
            $apiData->message   = "用户不存在";
            $apiData->code      = "3027";
            return $apiData->send();
        }

        $readPartyModel     = new ReadPartyModel();
        $readPartyInfo      = $readPartyModel->get_read_party_info($id);
        if($readPartyInfo->ReaParType == _geren_){
            if ($readPartyInfo->MemNumber != $readPartyManager->MemNumber) { //权限不足
                $apiData->message   = "不是管理员";
                $apiData->code      = "3096";
                return $apiData->send();
            }
        }elseif($readPartyInfo->ReaParType == _qiye_){ //获取该会员在该读书会下是否有打赏权限

            $userId         = $readPartyManager->UserID;
            $readMemberInfo = $readPartyModel->get_read_party_member_detail($userId,$id);
            if($readMemberInfo->reward_right == 0){
                $apiData->message   = "您没有打赏权利";
                $apiData->code      = "3096";
                return $apiData->send();
            }
        }



        //检查打赏的会员的数量
        $arrayIdList         = json_decode(urldecode($idlist));
        $msg            = sprintf("哟呵，%s给你打赏了%u个学习币",$readPartyManager->YourName,$amount);
        $messageModel   = new MessageModel();
        $msgId          = $messageModel->add_note_message("会长打赏",$msg);

        $memberModel= new MemberModel();
        foreach ($arrayIdList as $k => $memNum) {
            if(empty($memNum)){
                continue;
            }
            $memberInfo = $memberModel->get_member_info_by_member_number($memNum);
            //添加、发送信息
            $messageModel->add_user_message($msgId,$memberInfo->UserID);
            $jpush      = new Jpush();
            $jpush->alias = $memberInfo->UserName;
            $jpush->set_message($msg);
            $jpush->send();

            $readPartyModel->add_read_plan_money($id,$memNum,$amount);
            $data   = [
                'MoReLoType'    => 4,
                'MoReLoDesc'    => '打赏会员 ' . $amount . ' 个学习币。',
                'MoReLoBeiDesc' => '被会长打赏获得 ' . $amount . ' 个学习币。',
                'MoReLoMoney'   => $amount,
                'BeiMemNumber'  => $memNum,
                'MemNumber'     => $readPartyManager->MemNumber,
                'read_party_id' => $id
            ];
            $logId = $readPartyModel->add_money_change_log($data);
        }

        $apiData->message   = "打赏成功";
        $apiData->code      = "200";
        return $apiData->send();
    }


    /**
     * 清空学习币
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function resetMoneyAll(Request $request)
    {
        $readPartyId = $request->input('id');
        $idlist = $request->input('idlist');
        $mobile = $request->input('mobile');
        $key = $request->input('key');
        $sign = $request->input('sign');
        $timestamp = $request->input('timestamp');
        $client     = $request->header('Client-type');
        $apiData    = new ApiData();


        $params     = [
            "mobile"    => [$mobile,"缺少手机号","2037"],
            "key"       => [$key,"缺少设备ID","2038"],
            "id"        => [$readPartyId,"缺少读书会ID","6007"],
            "idlist"    => [$idlist,"缺少会员ID列表","6008"]
        ];


        $checkResult    = $this->check_params($params);
        if($checkResult->status == false){
            $apiData->message   = $checkResult->message;
            $apiData->code      = $checkResult->code;
            return $apiData->send();
        }
        $memberModel    = new MemberModel();
        $memberInfo     = $memberModel->get_member_info_by_mobile_key($mobile,$key);
        if($memberInfo == false){
            $apiData->message   = "会员不存在";
            $apiData->code      = "3027";
            return $apiData->send();
        }

        $readPartyModel = new ReadPartyModel();
        $readPartyInfo  = $readPartyModel->get_read_party_info($readPartyId);

        if(!$readPartyInfo){
            $apiData->message   = "读书会不存在";
            $apiData->code      = "3069";
            return $apiData->send();
        }



        if($client == 'android'){
            $idlist = json_decode(urldecode($idlist));
            $arrayidlist = [];
            foreach($idlist as $memId){
                array_push($arrayidlist,$memId);
            }
        }else{
            $arrayidlist = explode('_', $idlist);
            foreach ($arrayidlist as $k => $v) {
                if ($v == '') {
                    unset($arrayidlist[$k]);
                }
            }
        }


        foreach($arrayidlist as $memberNumber){
            //获取用户的积分
            $totalMoney = $readPartyModel->get_member_money($memberNumber,$readPartyId);
            //扣除积分
            $readPartyModel->reset_member_money($memberNumber,$readPartyId);
            //添加日志
            $data   = [
                'MoReLoType'    => 4,
                'MoReLoDesc'    => '扣除会员 ' . $totalMoney . ' 个学习币。',
                'MoReLoBeiDesc' => '被会长扣除 ' . $totalMoney . ' 个学习币。',
                'MoReLoMoney'   => -$totalMoney,
                'BeiMemNumber'  => $memberNumber,
                'MemNumber'     => $memberInfo->MemNumber,
                'read_party_id' => $readPartyId
            ];
            $readPartyModel->add_money_change_log($data);

            //添加、发送信息
            $readMemberInfo = $memberModel->get_member_info_by_member_number($memberNumber);
            $messageModel   = new MessageModel();
            $msg            = sprintf('"%s"读书会会长扣除了你的%u个学习币',$readPartyInfo->ReaParName,$totalMoney);
            $msgId          = $messageModel->add_note_message("会长扣除学习币",$msg);

            $messageModel->add_user_message($msgId,$readMemberInfo->UserID);
            $jpush      = new Jpush();
            $jpush->alias = $readMemberInfo->UserName;
            $jpush->set_message($msg);
            $r = $jpush->send();

        }


        return response()->json(['message' => 'ok', 'code' => '200']);
    }




    

    /**
     * 返回会员标示
     * @param string $mobile
     * @param string $appkey
     * @return mixed
     */
    protected function memNumber($mobile = '', $appkey = '')
    {
        $memberModel = new EntMemberInfo();
        $memberInfo  = $memberModel->get_member_info_by_mobile_and_appkey($mobile,$appkey);
        return $memberInfo;
    }
}